package com.zxx.ssm.controller;

import com.zxx.ssm.dao.SystemSetting;
import com.zxx.ssm.dao.SystemSettingExample;
import com.zxx.ssm.model.result.BizResponse;
import com.zxx.ssm.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("setting")
@Controller
public class SettingController {

    @Autowired
    private SettingService settingService;

    @RequestMapping(value = "getSettings", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse getSettings(@ModelAttribute SystemSetting setting) {
        BizResponse bizResponse = new BizResponse();
        SystemSettingExample systemSettingExample = new SystemSettingExample();
        SystemSettingExample.Criteria criteria = systemSettingExample.createCriteria();
        List<SystemSetting> systemSettings = settingService.selectByExample(systemSettingExample);
        if (systemSettings.size()<1) {
            bizResponse.put("settings", null);
        }else
            bizResponse.put("settings", systemSettings.get(0));
        return bizResponse;
    }

    @RequestMapping(value = "saveSettings", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse saveSettings(@ModelAttribute SystemSetting setting) {
        if (setting.getId() == null) {
            settingService.insertSelective(setting);
        }else {
            settingService.updateByPrimaryKeySelective(setting);
        }
        return new BizResponse();
    }
}
